% This function takes a date range , and longitude get the data from the
% ppefmL real-time caculator and returns the value
% web-app option: if flag = 1, http://ppefm.geomag.info/ppefmL?
% else http://electric-field.appspot.com?

function[fday, pp] = getppefmL(year,month,day ,nDays, longitude, flag)

if flag == 1,
    baseurl = 'http://ppefm.geomag.info/ppefmL?';
else,
    baseurl = 'http://electric-field.appspot.com/ppefmL?';
 end;

thisquerystring = sprintf('year=%d&month=%d&day=%d&utc=0&nDays=%d&startlong=%d&deltalong=360', ...
    year,month,day,nDays,longitude);

s = urlread([baseurl thisquerystring]);

fprintf('%s\n',[baseurl thisquerystring]);

s = strrep(s,'</br>', ' ');

% Find the start index of each rows
start_index = strfind(s,sprintf('%d %02d %02d',year,month,day));
%find the string </span> , which occures after the last data
end_index = strfind(s,'</span>') - 1;

data = sscanf(s(start_index(1):end_index),'%f',[6 Inf])';

pp = data(:,6);
fday = datenum(data(:,1),data(:,2),data(:,3),data(:,4),data(:,5),0);